
let initState = {
    arr:[],
    antarr : [ ],
}
// 第一步去写组件，第二步action，第三步，reducer
let tablereducer = (state=initState,{type,payload,ispull} )=>{
  switch(type){
      case "getlist" :
        let newArr = JSON.parse(JSON.stringify(state.arr)) // 不直接修改原来state中的数据，我们对原数据进行一个拷贝
        if (ispull == "shangla") {
            console.log(newArr.list,124);
            let newarr1 = newArr
            console.log(payload.list,125)
            let newarr2= payload.list
          newArr = [ ...newarr1,...newarr2]
            //  newArr = payload
        } else {
            newArr = payload.list
        }
      
        return {
          ...state,
          arr:newArr
        }


    case "getlantist" :
        let newantArr = JSON.parse(JSON.stringify(state.antarr)) 
        if (ispull == "shangla") {
            // console.log(newArr.list,124);
            let newarr3 = newantArr
            console.log(payload)
            let newarr4= payload
            newantArr = [ ...newarr3,...newarr4]
            //  newArr = payload
        } else {
            newantArr = payload
        }
        return {
            ...state,
            antarr:newantArr
        }


      case "delfn" :
        let delArr = JSON.parse(JSON.stringify(state.arr))// 如果数据不进行拷贝，就删不到
        delArr.splice(payload,1)
        return {
          ...state,
          arr:delArr
        }
      case "editfn" :
        let eidtarr = JSON.parse(JSON.stringify(state.arr))
       
        eidtarr[payload.index] = payload
        eidtarr[payload.index].key = new Date().getTime()

        return {
          ...state,
          arr:eidtarr
        }
      case "addfn" :
        payload.key = new Date().getTime()
        let addtarr = JSON.parse(JSON.stringify(state.arr))
        addtarr.push(payload)
        return {
          ...state,
          arr:addtarr
        }
      case "searchFn" :
        let newsearchArr = JSON.parse(JSON.stringify(state.arr))
       let searchresult =   newsearchArr.filter((item,index)=>{
          return item.tit.includes(payload)
        })
        return {
          ...state,
          arr:searchresult
        }
      default:
        return {
          ...state
        }
  }
  
}
export default tablereducer